import { Component, OnInit } from '@angular/core';
import { CartService } from '@app/services/cart/cart.service';
import { PcontentService } from '@app/services/pcontent/pcontent.service';
import { OrderService } from '@app/services/order/order.service';
import { SocketioService } from '@app/services/socketio/socketio.service';
import { storage } from '@app/utils';
import { tableId } from '@app/config';
import { Router } from '@angular/router';

@Component({
  selector: 'app-cart',
  templateUrl: './cart.component.html',
  styleUrls: ['./cart.component.scss']
})
export class CartComponent implements OnInit {
  private tableId: string = '';
  private cartList: any[] = [];
  private orderInfo: any = {};
  private allPrice: number = 0;
  private allNum: number = 0;
  constructor (
    private readonly cartService: CartService,
    private readonly orderService: OrderService,
    private readonly pcontentService: PcontentService,
    private readonly socketio: SocketioService,
    private readonly router: Router,
  ) {
    this.tableId = storage.getItem(tableId);
    console.log(storage.getItem(tableId))
    // 监听购物车数量变化
    this.socketio.socket.on('changeCard', (data: string) => {
      this.getCartList(this.tableId);
    })
  }

  ngOnInit() {
    this.getCartList(this.tableId);
    this.getOrderInfo(this.tableId);
  }

  // 获取订单信息
  private getOrderInfo(tableId: string): void {
    this.orderService.orderInfo$(tableId).subscribe(data => {
      const { code, message, result } = data;
      if (Object.is(code, 0)) {
        this.orderInfo = result;
      }
    })
  }

  // 获取购物车列表
  private getCartList(tableId: string): void {
    this.cartService.cartList$(tableId).subscribe(data => {
      const { code, message, result } = data;
      if (Object.is(code, 0)) {
        this.cartList = result;
        this.getTotalResult();
      }
    })
  }

  // 确认下单
  private confirmOrder(): void {
    console.log(this.cartList);
    console.log(this.orderInfo);
    const { personNum, tableId, remark } = this.orderInfo;
    const postData = {
      personNum,
      tableId,
      remark,
      goodsList: this.cartList.map(item => {
        const { goodsId, num, title, shopPrice } = item;
        return { goodsId, num, title, shopPrice };
      })
    };
    console.log(postData);
    this.orderService.confirmOrder$(postData).subscribe(data => {
      const { code, message, result } = data;
      if (Object.is(code, 0)) {
        this.router.navigate(['/order']);
      }
    })
  }
  // 减少数量
  private decNum(item: any, key: number) {
    const postData = {
      type: 0,
      tableId: this.tableId,
      goodsId: item.goodsId,
      num: item.num,
    };
    this.cartService.changeCartNum$(postData).subscribe(data => {
      const { code, message, result } = data;
      if (Object.is(code, 0)) {
        if (item.num > 1) {
          --item.num;
        } else {
          this.cartList.splice(key, 1);
        }
        this.getTotalResult();
        this.socketio.socket.emit('addCart', this.tableId);
      } else {
        console.log(message);
      }
    })
  }
  // 增加数量
  private incNum(item: any) {
    const postData = {
      type: 1,
      tableId: this.tableId,
      goodsId: item.goodsId,
      num: item.num,
    };
    this.cartService.changeCartNum$(postData).subscribe(data => {
      const { code, message, result } = data;
      if (Object.is(code, 0)) {
        ++item.num;
        this.getTotalResult();
        this.socketio.socket.emit('addCart', this.tableId);
      } else {
        console.log(message);
      }
    })
  }

  // 计算总数量和总价
  private getTotalResult(): void {
    let allPrice = 0;
    let allNum = 0;
    for (let i = 0; i < this.cartList.length; i++) {
      let currentItem = this.cartList[i];
      allPrice += currentItem.shopPrice * currentItem.num;
      allNum += currentItem.num;
    }
    this.allPrice = allPrice;
    this.allNum = allNum;
  }
}
